<template>
  <div class="common-layout">
    <el-container class="nav-side">
      <el-aside width="auto">
        <!-- #e1effe -->
        <el-menu
          :collapse="collapse"
          class="el-menu-vertical-demo"
          background-color="#181f31"
          text-color="#bfcbd9"
          router
          :default-active="activeMenu"
        >
          <div v-if="!collapse" class="login-text">
            <h1>后台管理系统</h1>
          </div>
          <TreeMenu :userMenu="userMenu" />
        </el-menu>
      </el-aside>
      <el-main>
        <el-container>
          <el-header>
            <div class="nav-left">
              <div
                class="menu-icon"
                @click="collapseChange"
                style="cursor: pointer"
              >
                <el-icon color="black" size="15" v-if="sideBar.collapse">
                  <Expand />
                </el-icon>
                <el-icon v-else color="black" size="15">
                  <Fold />
                </el-icon>
              </div>
              <BreadCrumb />
            </div>
            <div class="nav-right">
              <a href="#" @click="handleFullScreen"
                ><el-icon color="black" size="15">
                  <FullScreen />
                </el-icon>
              </a>
              <Settle />
              <UserAvatar :imgSrc="avatar" :imgSize="25" />
            </div>
          </el-header>
          <el-main class="box-cardView">
            <RouterView></RouterView>
          </el-main>
        </el-container>
      </el-main>
    </el-container>
  </div>
</template>

<script setup>
import avatar from '@/assets/images/human-resource.png'
import Settle from './Settle.vue'
import UserAvatar from './UserAvatar.vue'
import { ref, getCurrentInstance, provide } from 'vue'
const { $api } = getCurrentInstance().appContext.config.globalProperties
import { RouterView, useRouter } from 'vue-router'
import { useUserStore, useSidebarStore } from '@/stores/index'
import TreeMenu from './TreeMenu.vue'
import BreadCrumb from '@/components/BreadCrumb.vue'
import { Setting, FullScreen, Fold, Expand } from '@element-plus/icons-vue'
import { onMounted } from 'vue'

const userStore = useUserStore()
const sideBar = useSidebarStore()
// 使用provide将数仓库提供给子组件
provide('useUserStore', userStore)
// console.log(userInfo)
const router = useRouter()
const userMenu = ref([])
// 获取路由当前地址
const activeMenu = router.currentRoute.value.fullPath
// console.log(activeMenu)
onMounted(() => {
  // getNoticeCount()
  getMenuList()
})

let collapse = ref(false)

// 左侧菜单栏伸缩/展开
const collapseChange = () => {
  // sideBar.handleCollapse()
  collapse.value = !collapse.value
}

// 获取待通知数量
const getNoticeCount = async () => {
  try {
    const count = await $api.noticeCount()
    console.log(count)
    // userStore.saveNoticeCount(count)
  } catch (error) {
    console.error(error)
  }
}
// 获取菜单列表
const getMenuList = async () => {
  try {
    const { menuList, actionList } = await $api.getPermissionList()
    userStore.saveMenuList(menuList)
    userStore.saveActionList(actionList)
    userMenu.value = menuList
  } catch (error) {
    console.error(error)
  }
}

// 实现全屏功能
const handleFullScreen = () => {
  if (!document.fullscreenElement) {
    // 如果不在全屏模式下，则进入全屏
    if (document.documentElement.requestFullscreen) {
      document.documentElement.requestFullscreen()
    } else if (document.documentElement.mozRequestFullScreen) {
      document.documentElement.mozRequestFullScreen()
    } else if (document.documentElement.webkitRequestFullscreen) {
      document.documentElement.webkitRequestFullscreen()
    } else if (document.documentElement.msRequestFullscreen) {
      document.documentElement.msRequestFullscreen()
    }
  } else {
    // 如果在全屏模式下，则退出全屏
    if (document.exitFullscreen) {
      document.exitFullscreen()
    } else if (document.mozCancelFullScreen) {
      document.mozCancelFullScreen()
    } else if (document.webkitExitFullscreen) {
      document.webkitExitFullscreen()
    } else if (document.msExitFullscreen) {
      document.msExitFullscreen()
    }
  }
}
</script>

<style lang="scss">
.common-layout {
  position: relative;
  height: 100vh;

  //侧边栏
  .nav-side {
    position: fixed;
    width: 100%;
    height: 100vh;
    transition: width 0.5s;

    .login-text {
      width: 100%;
      height: 60px;
      display: flex;
      justify-content: center;
      align-items: center;
      background-color: #263445;

      h1 {
        font-size: 18px;
        white-space: nowrap;
        color: #f1f1f1;
      }
    }

    .el-aside {
      background-color: #181f31;
      overflow: hidden;
    }
  }

  // 右边主体内容
  .el-main {
    display: flex;
    flex-direction: column;
    height: 100%;

    .el-header {
      height: 4.375rem;
      background: #fff;
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      align-items: center;
      border-bottom: 1px solid #d8dce5;
      border-top: 1px solid #d8dce5;
      box-shadow: 0 0 20px 5px rgba(34, 84, 142, 0.26);

      .nav-left {
        display: flex;
        align-items: center;
      }

      .nav-right {
        width: 15rem;
        display: flex;
        align-items: center;
        justify-content: end;
      }

      .nav-right a {
        margin-right: 1.125rem;
      }

      .el-button {
        --el-button-bg-color: none;
      }
    }
  }

  .box-cardView {
    background-color: #f0f3f8;
    flex: auto;
  }

  //邮件徽章
  .message-badge {
    .el-button:focus,
    .el-button:hover {
      background-color: transparent;
    }

    .is-fixed {
      top: 12px !important;
      right: 28px !important;
    }

    .message {
      border: none;
      padding: 5px 20px;

      i {
        background-color: transparent;
        border: none;
        color: white;
        font-size: 27px;
      }
    }
  }
}

.el-drawer__title {
  text-align: center;
  /* 居中标题 */
}

//el-menu 自动缩放宽度
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0s;
}

.fade-enter,
.fade-leave-to

/* .fade-leave-active 在 <2.1.8 中以及被废弃 */ {
  opacity: 0;
}
</style>
